using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Tilemaps;

public class TilemapVisualizer : MonoBehaviour
{
    [SerializeField]
    public Tilemap backgroundTilemap, midgroundTileMap, propTileMapNoColliderTileMap, propTileMapWithColliderTileMap, foregroundTileMap;
    [SerializeField]
    private MapTiles mapTiles;

    public void PaintFloorTiles(IEnumerable<Vector2Int> floorPositions)
    {
        PaintTiles(backgroundTilemap, Util.GetRandomTByProbability(mapTiles.floorTiles.Select(x => x.tile).ToList(), mapTiles.floorTiles.Select(x => x.probability).ToList()), floorPositions);
    }

    private void PaintTiles(Tilemap tilemap, TileBase tile, IEnumerable<Vector2Int> positions)
    {
        foreach (var position in positions)
        {
            PaintSingleTile(tilemap, tile, position);
        }
    }
    public void PaintSingleBasicWall(Vector2Int position, string binaryType)
    {
        TileBase tile = null;
        switch (WallTypesHelper.GetBasicWallTileType(binaryType))
        {
            case WallTileType.Null:
                break;
            case WallTileType.WallTop:
                tile = Util.GetRandomTByProbability(mapTiles.wallTops.Select(x => x.tile).ToList(), mapTiles.wallTops.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallSideRight:
                tile = Util.GetRandomTByProbability(mapTiles.wallSideRights.Select(x => x.tile).ToList(), mapTiles.wallSideRights.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallSideLeft:
                tile = Util.GetRandomTByProbability(mapTiles.wallSideLefts.Select(x => x.tile).ToList(), mapTiles.wallSideLefts.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallBottom:
                tile = Util.GetRandomTByProbability(mapTiles.wallBottoms.Select(x => x.tile).ToList(), mapTiles.wallBottoms.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallFull:
                tile = Util.GetRandomTByProbability(mapTiles.wallFulls.Select(x => x.tile).ToList(), mapTiles.wallFulls.Select(x => x.probability).ToList());
                break;
            default:
                break;
        }
        if (tile != null)
        {
            PaintSingleTile(midgroundTileMap, tile, position);
        }
    }
    private void PaintSingleTile(Tilemap tilemap, TileBase tile, Vector2Int position)
    {
        Vector3Int tilePosition = tilemap.WorldToCell((Vector3Int)position);
        tilemap.SetTile(tilePosition, tile);
    }
    public void Clear()
    {
        backgroundTilemap.ClearAllTiles();
        midgroundTileMap.ClearAllTiles();
        propTileMapNoColliderTileMap.ClearAllTiles();
        propTileMapWithColliderTileMap.ClearAllTiles();
        foregroundTileMap.ClearAllTiles();
    }

    internal void PaintSingleCornerWall(Vector2Int position, string binaryType)
    {
        TileBase tile = null;
        switch (WallTypesHelper.GetWallTileType(binaryType))
        {
            case WallTileType.Null:
                break;
            case WallTileType.WallInnerCornerDownLeft:
                tile = Util.GetRandomTByProbability(mapTiles.wallInnerCornerDownLefts.Select(x => x.tile).ToList(), mapTiles.wallInnerCornerDownLefts.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallInnerCornerDownRight:
                tile = Util.GetRandomTByProbability(mapTiles.wallInnerCornerDownRights.Select(x => x.tile).ToList(), mapTiles.wallInnerCornerDownRights.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallDiagonalCornerDownLeft:
                tile = Util.GetRandomTByProbability(mapTiles.wallDiagonalCornerDownLefts.Select(x => x.tile).ToList(), mapTiles.wallDiagonalCornerDownLefts.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallDiagonalCornerDownRight:
                tile = Util.GetRandomTByProbability(mapTiles.wallDiagonalCornerDownRights.Select(x => x.tile).ToList(), mapTiles.wallDiagonalCornerDownRights.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallDiagonalCornerUpLeft:
                tile = Util.GetRandomTByProbability(mapTiles.wallDiagonalCornerUpLefts.Select(x => x.tile).ToList(), mapTiles.wallDiagonalCornerUpLefts.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallDiagonalCornerUpRight:
                tile = Util.GetRandomTByProbability(mapTiles.wallDiagonalCornerUpRights.Select(x => x.tile).ToList(), mapTiles.wallDiagonalCornerUpRights.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallFullEightDirections:
                tile = Util.GetRandomTByProbability(mapTiles.wallFulls.Select(x => x.tile).ToList(), mapTiles.wallFulls.Select(x => x.probability).ToList());
                break;
            case WallTileType.WallBottomEightDirections:
                tile = Util.GetRandomTByProbability(mapTiles.wallBottoms.Select(x => x.tile).ToList(), mapTiles.wallBottoms.Select(x => x.probability).ToList());
                break;
            default:
                break;
        }
        if (tile != null)
        {
            PaintSingleTile(midgroundTileMap, tile, position);
        }
    }
    public void PaintPropTile(Tile tile, Vector2Int pos, bool hasCollider)
    {
        if (hasCollider)
        {
            propTileMapWithColliderTileMap.SetTile((Vector3Int)pos, tile);
        }
        else
        {
            propTileMapNoColliderTileMap.SetTile((Vector3Int)pos, tile);
        }
    }
}
